#Butler core commands
from __future__ import print_function

import sys
import time
import subprocess


def butler_help(cmdstr, cmdargs):
    print('''
        ==Butler==
        Butler is a computer aide that executes human commands.

        For example, if you wanted to play a song, you could do:
            * play My Beloved
            * play My Beloved by Hungry Lucy

        Let's say that you are programming, but can't remember how that Python
        module works, you could type:
            * python documentation sys
            * pydoc sys
            * python doc sys

        Possible future:
        In the future Butler may be able to link your computers together, so
        you can quickly pull up information on one, while working on another.
        Butler may also get built-in speech recognition so you can just tell
        butler what you want.
        ''')


def butler_clear(cmdstr, cmdargs):
    print("\n" * 80)

def about_butler(a=None,b=None):
    print('''
Butler 0.0.1 alpha

Butler attempts to be discreet and unobtrusive, friendly but not familiar,
keenly anticipative of the needs of its employer, and graceful and precise
in execution of its duty.

Type 'help', 'copyright', or 'license' for more information.
    ''')

def butler_modules(cmdstr, cmdargs):
    output = ""

    for i in butler.possible_commands:
        output += "%19.19s " % (i)
    print( output )

def b_copyright(*args):
    print('''
    Butler is copyright 2011 Joseph Lewis <joehms22@gmail.com>.
    All rights reserved.

    Licensed under the MIT License.
    ''')

def b_license(*args):
    print('''
Butler is licensed under the MIT License:

Copyright (c) 2011 Joseph Lewis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

    ''')

def butler_registry(cmdstr, cmdargs):
    print(registry.dump())


def butler_repeat(cmdstr, cmdargs):
    #Command parse
    say = registry.fetch("butler.core.speech", False)

    try:
        if cmdargs[0] == "on":
            say = True
        else:
            say = False
    except:  #no command arguments, toggle
        say = not say

    #Store output
    registry.store("butler.core.speech", say)

#Redirect stdout to speech synthesis possibility
class repeater:
    '''Repeats text through speech synthesis.'''
    def write(*args):
        say = registry.fetch("butler.core.speech", False)

        print(*args[1:], end="", file=sys.saveout)  #Write to stdout first

        if say:
            if sys.platform == "linux2":
                #subprocess.call(["espeak", "\"%s\"" % (args[1:])], stderr=open('perr','w'), stdout=open('pout', 'w'))
                call("espeak", "\"%s\"" % (args[1:]))

            if sys.platform == "darwin":
                call("say", "\"%s\"" % (args[1:]))

sys.saveout = sys.stdout # Save stdout in a place all classes can find.
sys.stdout = repeater()  # Set stdout to speech synth if needed.



#Build dictionary.
TRIGGERS = {
                "help":butler_help,
                "copyright":b_copyright,
                "license":b_license,
                "clear":butler_clear,
                "about":about_butler,
                "plugins":butler_modules,
                "cmds":butler_modules,
                "commands":butler_modules,
                "ls":butler_modules,
                "reg":butler_registry,
                "repeat":butler_repeat,
                "speech":butler_repeat,
            }